package goActorsCommon

import(
	"os"
	"rpc"
	"strconv"
)

// Implements Pid.
// GoActorPid identifies a goActor.
type GoActorPid struct {
	// Identifies the goInstance running the goActor.
	Instance	* GoInstancePid
	// Identifies the goActor within the goInstance.
	GoActor		int32
}

func New_GoActorPid(instance * GoInstancePid, goActor int32) * GoActorPid {
	pid := &GoActorPid {
		Instance	: instance,
		GoActor		: goActor,
	}
	return pid
}

func ( gap *GoActorPid )Send( sender *GoActorPid, data []byte ) ( res bool, error os.Error ) {
	client, error := rpc.Dial( "tcp", gap.Instance.Ip + ":" + strconv.Itoa( gap.Instance.Port ) )
	defer client.Close()
	if error != nil {
		return false, error
	}

	var callRes bool
	error = client.Call( "GoInstance.Receive", New_MessageWrapper ( New_Message( sender, data ), gap.GoActor ), &callRes )
	if error != nil {
		return false, error
	}
	
	return callRes, nil
}

